`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company: 
// Engineer: 
// 
// Create Date:    18:32:28 03/27/2014 
// Design Name: 
// Module Name:    gamelogic 
// Project Name: 
// Target Devices: 
// Tool versions: 
// Description: 
//
// Dependencies: 
//
// Revision: 
// Revision 0.01 - File Created
// Additional Comments: 
//
//////////////////////////////////////////////////////////////////////////////////


module gamelogic( 	clk,
							up,
							down,
							forward,
							back,
							reset,
							enemy1_generation,
							enemy2_generation,
							enemy3_generation,
							enemy4_generation,
							enemy5_generation,
							enemy6_generation,
							bullet_enemy1,
							bullet_generation,
							bullet_stationary,
							bullet_generationleft,
							bullet_generationright,
							game_over,
							lives,
							level1,
							level2,
							level3,
							scores,
						   xpos_central, 
							ypos_central, 
							xpos_enemy1, 
							ypos_enemy1, 
							xpos_enemy2, 
							ypos_enemy2,
							xpos_enemy3, 
							ypos_enemy3,
							xpos_enemy4, 
							ypos_enemy4,
							xpos_enemy5, 
							ypos_enemy5,
							xpos_enemy6, 
							ypos_enemy6,
							xpos_bullet, 
							ypos_bullet,
							xpos_bullet_enemy1, 
							ypos_bullet_enemy1,
							xpos_bullet_stationary,
							ypos_bullet_stationary,
							xpos_bulletleft,
							ypos_bulletleft,
							xpos_bulletright,
							ypos_bulletright,
							xpos_collision,
							ypos_collision,
							xpos_collide_enemy1,
							ypos_collide_enemy1,
							xpos_collide_enemy2,
							ypos_collide_enemy2,
							enemy_signal1, 
							enemy_signal2, 
							bullet_signal,
							collide_enemy1,
							collide_enemy2,
							bullet_on,
							bullet_on1,
							collision
			            );


input up,down;
input forward,back;
input clk;
output reg enemy1_generation; //switch for enemy1 generation
output reg enemy2_generation; //switch for enemy2 generation
output reg enemy3_generation;
output reg enemy4_generation;
output reg enemy5_generation;
output reg enemy6_generation;
output reg bullet_generation; //switch for bullet generation
output reg bullet_enemy1;
output reg bullet_stationary;
input bullet_generationleft; 
input bullet_generationright; 
input reset;//switch for reset

//output reg level1,level2,level3;
output reg[9:0] xpos_central, ypos_central;
output reg[3:0] lives;
output reg game_over;
//output reg [4:0] scores;
output reg[9:0] xpos_enemy1, ypos_enemy1;
output reg[9:0] xpos_enemy2, ypos_enemy2;
output reg[9:0] xpos_enemy3, ypos_enemy3;
output reg[9:0] xpos_enemy4, ypos_enemy4;
output reg[9:0] xpos_enemy5, ypos_enemy5;
output reg[9:0] xpos_enemy6, ypos_enemy6;
output reg[9:0] xpos_bullet, ypos_bullet;
output reg[9:0] xpos_bullet_enemy1, ypos_bullet_enemy1;
output reg[9:0] xpos_collision,ypos_collision;
output reg[9:0] xpos_collide_enemy1,ypos_collide_enemy1;
output reg[9:0] xpos_collide_enemy2,ypos_collide_enemy2;
output reg[9:0] xpos_bulletleft, ypos_bulletleft;
output reg[9:0] xpos_bulletright, ypos_bulletright;
output reg[9:0] xpos_bullet_stationary, ypos_bullet_stationary;
output reg 	enemy_signal1,//enemy1 generation signal
				enemy_signal2, //enemy2 generation signal
				bullet_signal;
//output reg start;

output reg bullet_on=0;
output reg bullet_on1=0;

output reg [15:0] scores;
wire [9:0] position_x1,position_y1;
wire [9:0] position_x2,position_y2;
wire [9:0] position_x3,position_y3;
wire [9:0] position_x4,position_y4;
wire [9:0] position_x5,position_y5;
wire [9:0] position_x6,position_y6;
wire [9:0] position_x7,position_y7;
wire [9:0] position_x8,position_y8;
wire [9:0] position_x9,position_y9;
wire [9:0] position_x10,position_y10;
wire [9:0] position_x11,position_y11;
wire [9:0] position_x12,position_y12;
wire [9:0] position_x13,position_y13;

wire position_valid1,position_valid2,position_valid3,position_valid4,position_valid5;
wire position_valid13;
wire position_valid6,position_valid7,position_valid8,position_valid9,position_valid10,position_valid11,position_valid12;
output reg collision,collide_enemy1,collide_enemy2;

reg [20:0] freq_count_y, freq_count_g, freq_count_l;
reg [40:0] freq_count_z, freq_count_h, freq_count_m;
reg [80:0] freq_count_p, freq_count_i, freq_count_n;
reg [40:0] freq_count_a, freq_count_f, freq_count_k;
reg [40:0]	freq_count_w;
output reg level1,level2,level3;
reg reset1,reset2,p,t,flag,collide;



position_generation_rev2	pos_generation(	.clk(clk),
													.new_enemy_1(enemy1_generation),//enemy1 generation signal
													.new_enemy_2(enemy2_generation),//enemy2 generation signal
													.new_enemy_3(enemy3_generation),
													.new_enemy_4(enemy4_generation),
													.new_enemy_5(enemy5_generation),
													.new_enemy_6(enemy6_generation),
													.new_bullet_1(bullet_generation),
													.new_bullet_enemy_1(bullet_enemy1),
													.cha_pos_x(xpos_central),//character position signal
													.cha_pos_y(ypos_central),
													.position_x_1(position_x1),
													.position_y_1(position_y1),
													.position_x_2(position_x2),
													.position_y_2(position_y2),
													.position_x_3(position_x4),
													.position_y_3(position_y4),
													.position_x_4(position_x5),
													.position_y_4(position_y5),
													.position_x_5(position_x6),
													.position_y_5(position_y6),
													.position_x_6(position_x7),
													.position_y_6(position_y7),
												   .position_bullet_x(position_x3),
													.position_bullet_y(position_y3),
													.position_bullet_enemy_1_x(position_x13),
													.position_bullet_enemy_1_y(position_y13),
													
													.position_valid_1(position_valid1), //if enemy is in the right range
													.position_valid_2(position_valid2), //if enemy is in the right range
													.position_valid_3(position_valid3),//if bullet is in the right range
													.position_valid_enemy_1(position_valid13),
													.position_valid_4(position_valid4),
													.position_valid_5(position_valid5),
													.position_valid_6(position_valid6),
													.position_valid_7(position_valid7),
													.new_bullet_cha_horizontal(bullet_on),
                                       .new_bullet_cha_vertical(bullet_on1),
                                       .new_bullet_enemy_upright(xpos_enemy1),
                                       .new_bullet_enemy_upleft(ypos_enemy1),
                                       .new_bullet_enemy_vertical(bullet_stationary),
                                       .position_bullet_cha_horizontal_x(position_x8),
                                       .position_bullet_cha_horizontal_y(position_y8),
                                       .position_bullet_cha_vertical_x(position_x9),
                                       .position_bullet_cha_vertical_y(position_y9),
                                       .position_bullet_enemy_stationary_upright_x(position_x10),
                                       .position_bullet_enemy_stationary_upright_y(position_y10),
                                       .position_bullet_enemy_stationary_upleft_x(position_x11),
                                       .position_bullet_enemy_stationary_upleft_y(position_y11),
                                       .position_bullet_enemy_stationary_vertical_x(position_x12),
                                       .position_bullet_enemy_stationary_vertical_y(position_y12),
                                       .position_valid_cha_horizontal(position_valid8),
                                       .position_valid_cha_vertical(position_valid9),
                                       .position_valid_bullet_enemy_stationary_upright(position_valid10),
                                       .position_valid_bullet_enemy_stationary_upleft(position_valid11),
                                       .position_valid_bullet_enemy_stationary_vertical(position_valid12));

//scores_dec score ( scores,a,b,c,d);
//vga vga_1(	.clk_old(clk), 
//				.enable1(enemy_signal1),
//				.enable2(enemy_signal2), 
//				.enable3(bullet_signal), 
//				.red(red),
//				.green(green),
//				.blue(blue),
//				.HS(HS),
//				.VS(VS),
//				.X1(xpos_enemy1),
//				.Y1(ypos_enemy1),
//				.X2(xpos_enemy2),
//				.Y2(ypos_enemy2),
//				.X3(xpos_bullet),
//				.Y3(ypos_bullet),
//				.X4(xpos_central),
//				.Y4(ypos_central));




				
always@ (posedge clk) 
begin
	if(reset)
	begin
			xpos_central 	<= 10;
			ypos_central 	<= 240;
			xpos_collision <= 580;
			ypos_collision <= 220;
			xpos_collide_enemy1 <= 580;
			ypos_collide_enemy1 <= 220;
			xpos_collide_enemy2 <= 580;
			ypos_collide_enemy2 <= 220;
		
			scores			<= 16'b0000000000000101;
			lives				<=4'b0100;
			game_over		<=0;
			enemy_signal1	<=0;
			enemy_signal2	<=0;
			bullet_signal	<=0;
			//start				<=0;
			freq_count_y 	<=0;
			freq_count_z <=0;
			freq_count_p <=0;
			freq_count_a <=0;
			freq_count_w <=0;
			collision<=0;
			collide_enemy1<=0;
			collide_enemy2<=0;
			level1<=1;
			level2<=0;
			level3<=0;
			p<=0;
			t<=0;
			enemy1_generation<=0;
			enemy2_generation<=0;
			enemy3_generation<=0;
			enemy4_generation<=0;
			enemy5_generation<=0;
			enemy6_generation<=0;
			bullet_generation<=0;
			flag<=0;
			collide<=0;
			//count<=0;
	end 
	else if(level1 && ~level2 && ~level3)
	begin
	//start<=0;
	  // collision<=0;	
		//if(enemy1_generation) 
		if(freq_count_a==800000000)
		   begin
		enemy1_generation <= 1;
			if(position_valid1) 
			begin
				xpos_enemy1 <= position_x1; 
				ypos_enemy1 <= position_y1;  
			
			end
			else 
			begin
				enemy_signal1 <= 1;
			end
		end

		//if(enemy2_generation)
			if(freq_count_a==800000000 )
		begin
			enemy2_generation <= 1;
			if(position_valid2) 
			begin
				xpos_enemy2 <= position_x2; 
				ypos_enemy2 <= position_y2;  
			
			end
			else 
			begin
				enemy_signal2<=1;
			end
		end
		
		if(bullet_on) 
		begin
			//if(position_valid8) 
			//begin
				 xpos_bulletleft <= position_x8;
				 ypos_bulletleft <= position_y8; 
				
			//end
			//else 
			//begin
			//	bullet_signal<=1;
			//end
		end
		
		if(bullet_on1) 
		begin
			if(position_valid9) 
			begin
				 xpos_bulletright <= position_x9;
				 ypos_bulletright <= position_y9; 
				
			end
			else 
			begin
				bullet_signal<=1;
			end
		end
		
	if(freq_count_a==800000000)
		   begin
			bullet_enemy1 <=1;
			if(position_valid13) 
			begin
				 xpos_bullet_enemy1 <= position_x13;
				 ypos_bullet_enemy1 <= position_y13; 
				
			end
			else 
			begin
				bullet_signal<=1;
	//			bullet_generation<=0;
			end
		end     
		
		//scoreboard
		
		
		//central character control
		
		if (freq_count_y == 1680000)
		begin
			freq_count_y <= 0;
			
		end
		else
			freq_count_y <= freq_count_y + 1;
	
		
		if (freq_count_z == 100000000)
		
		begin
			freq_count_z <= 0;
		
		end
		else
			freq_count_z <= freq_count_z + 1;
	
	
	//	if(flag==1) begin
		
		if (freq_count_p == 1200000000)
		begin
			freq_count_p <= 0;
		
		end
		else
			freq_count_p <= freq_count_p + 1;    
			
		if (freq_count_a == 800000000)
		begin
			freq_count_a <= 800000000;
		
		end
		else
			freq_count_a <= freq_count_a + 1;
		
				
		if (freq_count_w == 800000000)
		begin
			freq_count_w <= 800000000;
		
		end
		else
			freq_count_w <= freq_count_w + 1;
		
		
		if (down==1 & freq_count_y ==0)
			ypos_central <= ypos_central + 1; 
		
		if (up==1 & freq_count_y ==0)
			ypos_central <= ypos_central - 1; 
			
		if (forward==1 & freq_count_y ==0)
			xpos_central <= xpos_central + 1; 
			
		if (back==1 & freq_count_y ==0)
			xpos_central <= xpos_central - 2; 
			
		if(ypos_central == 280) begin
			ypos_central <= 279; end
			
		else if(ypos_central < 100) begin
			ypos_central <= 279;		 end
			
		else if(xpos_central == 600) begin
			xpos_central <= 1; end
		
		else if(xpos_central < 2) begin
			xpos_central <= 2;		end
		
		else begin
		end
	
		if(freq_count_z==0)
			scores <= scores + 16'b0000000000000101;
			
		if(lives<1)
			game_over<=1;
		
		if(scores<5)
			scores <=5;
			
		if(scores>200) begin
			level2<=1;
			level1<=0;
			reset1<=1; 
			reset2<=0;
			enemy1_generation<=0;
			enemy2_generation<=0;
			
			end
		
	/*if((xpos_enemy1 - xpos_central <= 47 && ypos_enemy1 - ypos_central <= 45 ) || 
	  (xpos_enemy2 - xpos_central <= 47 && ypos_enemy2 - ypos_central <= 48 ) ||
	  (xpos_bullet - xpos_central <= 26 && ypos_bullet - ypos_central <= 17 ))*/
	if(((xpos_enemy1 - xpos_central <= 47 && ypos_enemy1 - ypos_central <= 45) && ~collide) ||
		((xpos_central - xpos_enemy1 <= 47 && ypos_central - ypos_enemy1 <= 45) && ~collide))
	begin
	lives<=lives-1;
	scores<=scores-50;
	collision<=1;
	//flag<=1;
	collide<=1;
	xpos_collision<=xpos_central;
	ypos_collision<=ypos_central;
	enemy1_generation<=0;
	enemy2_generation<=0;
	//bullet_generationleft<=0;
//	bullet_generationright<=0;
	bullet_enemy1<=0;
	//freq_count_a<=0;
	//enemy_signal1<=1;
	//enemy_signal2<=1;
	//bullet_signal<=1;
   end
	
	if(((xpos_enemy2 - xpos_central <= 47 && ypos_enemy2 - ypos_central <= 48) && ~collide) ||
		((xpos_central - xpos_enemy2 <= 47 && ypos_central - ypos_enemy2 <= 48) && ~collide))
	begin
	lives<=lives-1;
	scores<=scores-50;
	collision<=1;
	//flag<=1;
	collide<=1;
	enemy2_generation<=0;
	enemy1_generation<=0;
//	freq_count_a<=0;
	xpos_collision<=xpos_central;
	ypos_collision<=ypos_central;
	end
	
	if ( xpos_bullet - xpos_central <= 26 && ypos_bullet - ypos_central <= 17 && ~collide )
	begin
	lives<=lives-1;
	scores<=scores-50;
	collision<=1;
	collide<=1;
	//xpos_central<=10;
	//ypos_central<=20;
	xpos_collision<=xpos_bullet;
	ypos_collision<=ypos_bullet;
	end
	
	/*if(bullet_generationleft && count==0) begin
			bullet_on <=1;
			count<=1;
			end
	else if( collide_enemy1 || collide_enemy2 || xpos_bulletleft>640) begin
			bullet_on<=0; 
			count<=0;   end
		
    if(bullet_generationright) begin
			bullet_on1 <=1;
			end  
	//	else bullet_on1<=0;	*/
	
	if(bullet_generationleft) begin
	bullet_on<=1; end
	if(bullet_generationright) begin
	bullet_on1<=1; end
	
	
	if(bullet_on) begin
	if(( xpos_enemy1 - xpos_bulletleft <= 15 && ypos_bulletleft - ypos_enemy1 <= 45 ) ||
	    ( xpos_enemy1 - xpos_bulletleft <= 15 && ypos_enemy1 - ypos_bulletleft <= 45 ))
	begin
//	scores<= scores + 30;
//	collision<=1;
   collide_enemy1<=1;
	xpos_collide_enemy1<=xpos_enemy1;
   ypos_collide_enemy1<=ypos_enemy1;
	bullet_on<=0;
  	enemy1_generation <= 0;
	end end
	
	if(bullet_on) begin
if(( xpos_enemy2 - xpos_bulletleft <= 25 && ypos_bulletleft - ypos_enemy2 <= 50 ) ||
    ( xpos_enemy2 - xpos_bulletleft <= 25 && ypos_enemy2 - ypos_bulletleft <= 50))

   begin
	//scores<= scores + 30;
	//collision<=1;
    collide_enemy2<=1;
   xpos_collide_enemy2<=xpos_enemy2;
	ypos_collide_enemy2<=ypos_enemy2;
	bullet_on<=0;
   enemy2_generation <= 0;
	end end
	
	//if(collide_enemy1==1) begin
//	enemy1_generation <=0; end
	
//	if(collide_enemy2==1) begin
	//enemy2_generation <=0; end
	
	if(collide_enemy1==1) begin
	if(freq_count_w==0) begin
	collide_enemy1<=0;
	//bullet_on<=0;
	//enemy1_generation<=0;
	freq_count_w<=0;
	end end 
	
	if(collide_enemy2==1) begin
	if(freq_count_w==0) begin
	collide_enemy2<=0;
	enemy2_generation<=0;
	bullet_on<=0;
	freq_count_w<=0;
	end end 
	
	if(collision==1) begin
   if(freq_count_p==0) begin
	xpos_central<=10;
   ypos_central<=240;  
		collision<=0;
		collide<=0;
		enemy1_generation<=0;
		enemy2_generation<=0;
	//	freq_count_a <= 800000000;
		//flag<=1;
		freq_count_p<=0;
	   end
	   end
//	else begin
//	collision<=0; end
	//else begin
	//collision<=0; end
	//	if(up)
	//		ypos_central = ypos_central + 1;
	//	else if(down)
	//		ypos_central = ypos_central - 1;
	//	else 
	//		ypos_central = ypos_central;

		end	
		
	
else if (level2 && ~level3 && ~level1)
begin

if(reset1 && ~p) begin
xpos_central<=10;
ypos_central<=240;
enemy_signal1	<=0;
enemy_signal2	<=0;
bullet_signal	<=0;
freq_count_g 	<=0;
freq_count_h <=0;
freq_count_i <=0;
freq_count_f <=0;
p<=1;
collision<=0;
collide<=0;
flag<=0;
end

else begin

		
		if(freq_count_f==800000000 )
		begin
			enemy3_generation <= 1;
			if(position_valid4) 
			begin
				xpos_enemy3 <= position_x4; 
				ypos_enemy3 <= position_y4;  
			
			end
			else 
			begin
				enemy_signal2<=1;
			end
		end
		
			if(freq_count_f==800000000 )
		begin
			enemy4_generation <= 1;
			if(position_valid5) 
			begin
				xpos_enemy4 <= position_x5; 
				ypos_enemy4 <= position_y5;  
			
			end
			else 
			begin
				enemy_signal2<=1;
			end
		end
		
		
	/*	if(bullet_generation) 
		begin
			if(position_valid3) 
			begin
				 xpos_bullet <= position_x3;
				 ypos_bullet <= position_y3; 
				
			end
			else 
			begin
				bullet_signal<=1;
			end
		end   */
		//scoreboard
		
		
		//central character control
		
		if (freq_count_g == 1680000)
		begin
			freq_count_g <= 0;
			
		end
		else
			freq_count_g <= freq_count_g + 1;
	
		
		if (freq_count_h == 100000000)
		
		begin
			freq_count_h <= 0;
		
		end
		else
			freq_count_h <= freq_count_h + 1;
	
	
		if (freq_count_i == 1200000000)
		begin
			freq_count_i <= 0;
		
		end
		else
			freq_count_i <= freq_count_i + 1;    
			
		if (freq_count_f == 800000000)
		begin
			freq_count_f <= 800000000;
		
		end
		else
			freq_count_f <= freq_count_f + 1;
		
		
		if (down==1 & freq_count_g ==0)
			ypos_central <= ypos_central + 1; 
		
		if (up==1 & freq_count_g ==0)
			ypos_central <= ypos_central - 1; 
			
		if (forward==1 & freq_count_g ==0)
			xpos_central <= xpos_central + 2; 
			
		if (back==1 & freq_count_g ==0)
			xpos_central <= xpos_central - 3; 
			
		if(ypos_central == 280) begin
			ypos_central <= 279; end
			
		else if(ypos_central < 100) begin
			ypos_central <= 279;		 end
			
	//	else if(xpos_central == 600) begin
	//		xpos_central <= 1; end
	
	else if(xpos_central == 598) begin
			xpos_central <= 1; end
		
		//else if(xpos_central < 1) begin
	//		xpos_central <= 1;		end
	
	else if(xpos_central < 3) begin
		xpos_central <= 3;		end
		
		else begin
		end
	
		if(freq_count_h==0)
			scores <= scores + 16'b0000000000000101;
			
		if(lives<1)
			game_over<=1;
		
		if(scores<5)
			scores <=5;
			
		if(scores>300) begin
			reset2<=1;
			level3<=1; 
			level2<=0;
			enemy3_generation<=0;
			enemy4_generation<=0;
		 end
		
	if( xpos_enemy3 - xpos_central <= 40 && ypos_enemy3 - ypos_central <= 32 && ~collide )
	begin
	lives<=lives-1;
	scores<=scores-50;
	collision<=1;
	collide<=1;
	flag<=1;
	//xpos_central<=10;
	//ypos_central<=20;
	xpos_collision<=xpos_central;
	ypos_collision<=ypos_central;
	enemy1_generation<=0;
	enemy2_generation<=0;
   end
	
	if ( xpos_enemy4 - xpos_central <= 49 && ypos_enemy4 - ypos_central <= 43 && ~collide )
	begin
	lives<=lives-1;
	scores<=scores-50;
	collision<=1;
	collide<=1;
	flag<=1;
	enemy3_generation<=0;
	enemy4_generation<=0;
	//xpos_central<=10;
	//ypos_central<=20;
	xpos_collision<=xpos_central;
	ypos_collision<=ypos_central;
	end
	
	if ( xpos_bullet - xpos_central <= 26 && ypos_bullet - ypos_central <= 17 && ~collide )
	begin
	lives<=lives-1;
	scores<=scores-50;
	collision<=1;
	collide<=1;
	xpos_collision<=xpos_central;
	ypos_collision<=ypos_central;
	end
	
	
	if(collision==1) begin
   if(freq_count_i==0) begin
	xpos_central<=10;
	ypos_central<=240;  
		collision<=0;
		collide<=0;
		freq_count_i<=0;
		flag<=0;
	enemy3_generation<=0;
	enemy4_generation<=0;
	   end
		end

		end
		end

else if (level3 && ~level1 && ~level2)
begin

if(reset2 && ~t) begin
xpos_central<=10;
ypos_central<=240;
enemy_signal1	<=0;
enemy_signal2	<=0;
bullet_signal	<=0;
freq_count_k 	<=0;
freq_count_l <=0;
freq_count_m <=0;
freq_count_n <=0;
t<=1;
collision<=0;
collide<=0;
flag<=0;
end

else begin
if(freq_count_k==800000000)
		   begin
		enemy5_generation <= 1;
			if(position_valid6) 
			begin
				xpos_enemy5 <= position_x6; 
				ypos_enemy5 <= position_y6;  
			
			end
			else 
			begin
				enemy_signal1 <= 1;
			end
		end

		//if(enemy2_generation)
			if(freq_count_k==800000000 )
		begin
			enemy6_generation <= 1;
			if(position_valid7) 
			begin
				xpos_enemy6 <= position_x7; 
				ypos_enemy6 <= position_y7;  
			
			end
			else 
			begin
				enemy_signal2<=1;
			end
		end
		
		
		if(freq_count_k==800000000)
		   begin
			bullet_generation <=1;
			if(position_valid3) 
			begin
				 xpos_bullet <= position_x3;
				 ypos_bullet <= position_y3; 
				
			end
			else 
			begin
				bullet_signal<=1;
			//	bullet_generation<=0;
			end
		end   
	
		if(freq_count_k==800000000) begin
		bullet_stationary<=1;
	
			if(position_valid12) 
			begin
				 xpos_bullet_stationary <= position_x12;
				 ypos_bullet_stationary <= position_y12; 
				
			end
			else 
			begin
				bullet_signal<=1;
			end
  end
		//scoreboard
		
		
		//central character control
		
		if (freq_count_l == 1680000)
		begin
			freq_count_l <= 0;
			
		end
		else
			freq_count_l <= freq_count_l + 1;
	
		
		if (freq_count_m == 100000000)
		
		begin
			freq_count_m <= 0;
		
		end
		else
			freq_count_m <= freq_count_m + 1;
	
		if (freq_count_n == 1200000000)
		begin
			freq_count_n <= 0;
		
		end
		else
			freq_count_n <= freq_count_n + 1;
			
		if (freq_count_k == 800000000)
		begin
			freq_count_k <= 800000000;
		
		end
		else
			freq_count_k <= freq_count_k + 1;
		
		
		if (down==1 & freq_count_l ==0)
			ypos_central <= ypos_central + 2; 
		
		if (up==1 & freq_count_l ==0)
			ypos_central <= ypos_central - 2; 
			
		if (forward==1 & freq_count_l ==0)
			xpos_central <= xpos_central + 3; 
			
		if (back==1 & freq_count_l ==0)
			xpos_central <= xpos_central - 4; 
			
		if(ypos_central == 280) begin
			ypos_central <= 279; end
			
		else if(ypos_central < 100) begin
			ypos_central <= 279;		 end
			
		else if(xpos_central == 597) begin
			xpos_central <= 1; end
		
		else if(xpos_central < 4) begin
			xpos_central <= 6;		end
		
		else begin
		end
	
		if(freq_count_m==0)
			scores <= scores + 16'b0000000000000101;
			
		if(lives<1)
			game_over<=1;
		
		if(scores<5)
			scores <=5;
		
	if( xpos_enemy5 - xpos_central <= 50 && ypos_enemy5 - ypos_central <= 50 && ~collide)
	begin
	lives<=lives-1;
	scores<=scores-50;
	collision<=1;
	xpos_collision<=xpos_central;
	ypos_collision<=ypos_central;
	collide<=1;
	flag<=1;
	enemy5_generation<=0;
	enemy6_generation<=0;
   end
	
	if ( xpos_enemy6 - xpos_central <= 50 && ypos_enemy6 - ypos_central <= 50 && ~collide)
	begin
	lives<=lives-1;
	scores<=scores-50;
	collision<=1;
	xpos_collision<=xpos_central;
	ypos_collision<=ypos_central;
	collide<=1;
	flag<=1;
	enemy5_generation<=0;
	enemy6_generation<=0;
	end
	
	if ( xpos_bullet - xpos_central <= 26 && ypos_bullet - ypos_central <= 17)
	begin
	lives<=lives-1;
	scores<=scores-50;
	collision<=1;
	xpos_collision<=xpos_central;
	ypos_collision<=ypos_central;
	end
	
	if(collision==1) begin
   if(freq_count_n==0) begin
	 xpos_central<=10;
	 ypos_central<=240;  
		collision<=0;
		collide<=0;
		freq_count_n<=0;
		flag<=0;
	enemy5_generation<=0;
	enemy6_generation<=0;
	   end
		end

		end    
		end	
		end
		endmodule


/* if((xpos_enemy1 - xpos_central>=39 && xpos_enemy1 - xpos_central <= 40 && ypos_enemy1 - ypos_central <= 40 ) || 
	  (xpos_enemy2 - xpos_central>=39 && xpos_enemy2 - xpos_central <= 40 && ypos_enemy2 - ypos_central <= 40 ) ||
	  (xpos_bullet - xpos_central <= 20 && ypos_bullet - ypos_central <= 20 )) */
